package components.base
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.controls.DataGrid;
	import mx.events.FlexEvent;
	
	import components.custom.CheckBoxColumn;
	import components.custom.CheckBoxColumnHeaderRenderer;
	import components.custom.CheckBoxItemRenderer;
	
	import framework.LangMgr;
	
	import interfaces.IDispose;
	
	[Event(name="selectedOrUnselectedAll", type="flash.events.Event")]
	[Event(name="changeCurrentState", type="flash.events.Event")]
	public class BaseDataGrid extends DataGrid implements IDispose
	{
		public function BaseDataGrid()
		{
			super();
			rowHeight = 27;
			variableRowHeight = true;
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			addEventListener(CheckBoxItemRenderer.CHANGE_CHECKED_STATUS, changeCheckedStateHandler);
			addEventListener(CheckBoxColumnHeaderRenderer.SELECTED_OR_UNSELECTED_ALL, selectedStateHandler);
			addEventListener(CheckBoxColumnHeaderRenderer.ADD_HEADER, addHeaderHandler);
		}
		
		/**
		 * 批量设置选中项
		 * @param p_checkBoxes
		 * 
		 */
		[Bindable("change")]
		[Bindable("valueCommit")]
		public function set checkedItems(p_checkBoxes:Array):void
		{
			var iList:IList = dataProvider as IList;
			if(!iList)return;
			for(var i:int = 0;i < iList.length;i++){
				if(!iList[i].hasOwnProperty("checked"))
					continue;
				if(p_checkBoxes.indexOf(iList[i]) < 0)
					iList[i].checked = false;
				else
					iList[i].checked = true;
			}
			dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
		}
		public function get checkedItems():Array
		{
			var iList:IList = dataProvider as IList;
			var _checkedItems:Array = [];
			for(var i:int = 0;i < iList.length;i++){
				var obj:Object = iList[i];
				if(obj.hasOwnProperty("checked") && obj.checked)
					_checkedItems.push(obj);
			}
			return _checkedItems;
		}
		
		private var _header:CheckBoxColumnHeaderRenderer;
		public function get checkBoxHeader():CheckBoxColumnHeaderRenderer
		{
			return _header;
		}
		private function addHeaderHandler(p_evt:Event):void
		{
			p_evt.stopPropagation();
			_header = p_evt.target as CheckBoxColumnHeaderRenderer;
		}
		
		/**
		 * 子项checkBox column选中状态改变
		 * @param p_evt
		 * 
		 */	
		private function changeCheckedStateHandler(p_evt:Event):void
		{
			p_evt.stopPropagation();
			var cbItemRenderer:CheckBoxItemRenderer = p_evt.target as CheckBoxItemRenderer;
			if(!cbItemRenderer.selected && _header.selected)
				_header.selected = false;
			dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
		}
		
		
		/**
		 * 全选与全不选改变
		 * @param p_evt
		 * 
		 */		
		private function selectedStateHandler(p_evt:Event):void
		{
			p_evt.stopPropagation();
			var cbBox:CheckBoxColumnHeaderRenderer = p_evt.target as CheckBoxColumnHeaderRenderer;
			if(!cbBox)return;
			var arrc:ArrayCollection = dataProvider as ArrayCollection;
			if(!arrc)return;
			if(cbBox.selected)
				checkedItems = arrc.source;
			else
				checkedItems = [];
		}
		
		/**
		 * 控件创建完成后调用初始化信息
		 */
		private function creationCompleteHandler(p_evt:FlexEvent):void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			resourcesChanged();
		}
		
		override public function stylesInitialized():void
		{
			if(getStyle("headerStyleName") == undefined){
				headerHeight = 25;
				setStyle("headerStyleName", "dataGridHeaderStyle");
			}
			if(styleName == null)
				styleName = "dataGrid";
			super.stylesInitialized();
		}
		
		/**
		 * 实现国际化
		 */
		override protected function resourcesChanged():void
		{
			super.resourcesChanged();
			var len:int = columnCount;
			for (var index:int = 0;index < len;index++){
				if(columns[index] is CheckBoxColumn || 
					!columns[index].hasOwnProperty("locale") ||
					columns[index]["locale"] == null)
					continue;
				var str:String = columns[index].locale;
				//获取国际化字符 串
				if(!str)continue;
				str = LangMgr.getResourceByLocaleKey(str);
				//过滤冒号
				str = str.replace(/:|：/g, "");
				columns[index].headerText = (str != null && str != "undefined") ? str : "";
			}
		}
		
		public function dispose():void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			removeEventListener(CheckBoxItemRenderer.CHANGE_CHECKED_STATUS, changeCheckedStateHandler);
			removeEventListener(CheckBoxColumnHeaderRenderer.SELECTED_OR_UNSELECTED_ALL, selectedStateHandler);
			removeEventListener(CheckBoxColumnHeaderRenderer.ADD_HEADER, addHeaderHandler);
		}
	}
}